﻿using System.Collections.Generic;
using WeiboWPSdk.Json;
using WeiboWPSdk.Json.Linq;
using WeiboWPSdk.ResponseEntity;
using System.Linq;
using System.Threading.Tasks;

namespace WeiboWPSdk.Api {
    public class ShortUrlApi : ApiBase {
        public ShortUrlApi(ClientHttpHelper httpHelper)
            : base(httpHelper) { 
        }

        #region 转换接口
        /// <summary>
        /// 将一个或多个长链接转换成短链接
        /// http://open.weibo.com/wiki/2/short_url/shorten
        /// 多个url参数需要使用如下方式：url_long=aaa&url_long=bbb
        /// </summary>
        /// <param name="urlLong">需要转换的长链接，需要URLencoded，最多不超过20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Url>> ShortenAsync(params string[] urlLong) {
            List<WeiboParameter> parameters = new List<WeiboParameter>();
            foreach (var u in urlLong) {
                parameters.Add(new WeiboParameter("url_long", u));
            }
            var json = await HttpHelper.GetBasicCommandAsync("short_url/shorten", parameters.ToArray());
            return JsonConvert.DeserializeObject<IEnumerable<Url>>(JObject.Parse(json)["urls"].ToString());
        }
        /// <summary>
        /// 将一个或多个短链接还原成原始的长链接
        /// http://open.weibo.com/wiki/2/short_url/expand
        /// 多个url参数需要使用如下方式：url_short=aaa&url_short=bbb
        /// </summary>
        /// <param name="urlShort">需要还原的短链接，需要URLencoded，最多不超过20个 。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Url>> ExpandAsync(params string[] urlShort) {
            List<WeiboParameter> parameters = new List<WeiboParameter>();
            foreach (var u in urlShort) {
                parameters.Add(new WeiboParameter("url_short", u));
            }
            var json = await HttpHelper.GetBasicCommandAsync("short_url/expand", parameters.ToArray());
            return JsonConvert.DeserializeObject<IEnumerable<Url>>(JObject.Parse(json)["urls"].ToString());
        }
        #endregion
        #region 数据接口
        /// <summary>
        /// 获取短链接的总点击数
        /// http://open.weibo.com/wiki/2/short_url/clicks
        /// 多个url参数需要使用如下方式：url_short=aaa&url_short=bbb；
        /// </summary>
        /// <param name="url_short">需要取得点击数的短链接，需要URLencoded，最多不超过20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<int>> GetClicksAsync(params string[] urlShort) {
            List<WeiboParameter> parameters = new List<WeiboParameter>();
            foreach (var u in urlShort) {
                parameters.Add(new WeiboParameter("url_short", u));
            }
            var json = await HttpHelper.GetBasicCommandAsync("short_url/clicks", parameters.ToArray());
            return JsonConvert.DeserializeObject<IEnumerable<UrlClicks>>(JObject.Parse(json)["urls"].ToString()).Select(p => p.Clicks);
        }
        /// <summary>
        /// 获取一个短链接点击的referer来源和数量
        /// http://open.weibo.com/wiki/2/short_url/referers
        /// </summary>
        /// <param name="url_short">需要取得点击来源的短链接，需要URLencoded。</param>
        /// <returns></returns>
        public async Task<IEnumerable<UrlReferer>> GetReferersAsync(string urlShort) {
            var json = await HttpHelper.GetBasicCommandAsync("short_url/referers", new WeiboParameter("url_short", urlShort));
            return JsonConvert.DeserializeObject<IEnumerable<UrlReferer>>(JObject.Parse(json)["referers"].ToString());
        }
        /// <summary>
        /// 获取一个短链接点击的地区来源和数量
        /// http://open.weibo.com/wiki/2/short_url/locations
        /// </summary>
        /// <param name="urlShort">需要取得点击地区的短链接，需要URLencoded。</param>
        /// <returns></returns>
        public async Task<IEnumerable<UrlLocation>> GetLocations(string urlShort) {
            var json = await HttpHelper.GetBasicCommandAsync("short_url/locations", new WeiboParameter("url_short", urlShort));
            return JsonConvert.DeserializeObject<IEnumerable<UrlLocation>>(JObject.Parse(json)["locations"].ToString());
        }
        /// <summary>
        /// 获取短链接在微博上的微博分享数
        /// http://open.weibo.com/wiki/2/short_url/share/counts
        /// 多个url参数需要使用如下方式：url_short=aaa&url_short=bbb；
        /// </summary>
        /// <param name="url_short">需要取得分享数的短链接，需要URLencoded，最多不超过20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<int>> GetShareCounts(params string[] urlShort) {
            List<WeiboParameter> parameters = new List<WeiboParameter>();
            foreach (var u in urlShort) {
                parameters.Add(new WeiboParameter("url_short", u));
            }
            var json = await HttpHelper.GetBasicCommandAsync("short_url/share/counts", parameters.ToArray());
            return JsonConvert.DeserializeObject<IEnumerable<UrlShareCounts>>(JObject.Parse(json)["urls"].ToString()).Select(p => p.ShareCounts);
        }
        /// <summary>
        /// 获取包含指定单个短链接的最新微博内容
        /// </summary>
        /// <param name="urlShort">需要取得关联微博内容的短链接，需要URLencoded。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最多不超过200。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Status>> GetShareStatuses(string urlShort, long sinceId = 0, long maxId = 0, int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("short_url/share/statuses", new WeiboParameter("url_short", urlShort), new WeiboParameter("since_id", sinceId),
                new WeiboParameter("max_id", maxId), new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<IEnumerable<Status>>(JObject.Parse(json)["share_statuses"].ToString());
        }
        /// <summary>
        /// 获取短链接在微博上的微博评论数
        /// http://open.weibo.com/wiki/2/short_url/comment/counts
        /// 多个url参数需要使用如下方式：url_short=aaa&url_short=bbb；
        /// </summary>
        /// <param name="urlShort">需要取得分享数的短链接，需要URLencoded，最多不超过20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<int>> GetCommentCounts(params string[] urlShort) {
            List<WeiboParameter> parameters = new List<WeiboParameter>();
            foreach (var u in urlShort) {
                parameters.Add(new WeiboParameter("url_short", u));
            }
            var json = await HttpHelper.GetBasicCommandAsync("short_url/comment/counts", parameters.ToArray());
            return JsonConvert.DeserializeObject<IEnumerable<UrlCommentCounts>>(JObject.Parse(json)["urls"].ToString()).Select(p => p.CommentCounts);
        }
        /// <summary>
        /// 获取包含指定单个短链接的最新微博评论
        /// http://open.weibo.com/wiki/2/short_url/comment/comments
        /// </summary>
        /// <param name="urlShort">需要取得关联微博内容的短链接，需要URLencoded。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的微博，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50，最多不超过200。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Comment>> GetShareComments(string urlShort, long sinceId = 0, long maxId = 0, int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("short_url/comment/comments", new WeiboParameter("url_short", urlShort), new WeiboParameter("since_id", sinceId),
                new WeiboParameter("max_id", maxId), new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<IEnumerable<Comment>>(JObject.Parse(json)["share_comments"].ToString());
        }
        #endregion
    }
}
